package com.jsmile.cloud.accountcenter.shop.service;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jsmile.cloud.accountcenter.api.enums.AccountCodeEnum;
import com.jsmile.cloud.accountcenter.api.exception.AccountException;
import com.jsmile.cloud.accountcenter.api.req.shop.ReqSupplyCreate;
import com.jsmile.cloud.accountcenter.api.req.shop.ReqSupplyQuery;
import com.jsmile.cloud.accountcenter.api.req.shop.ReqSupplyUpdate;
import com.jsmile.cloud.accountcenter.api.res.ResSupply;
import com.jsmile.cloud.accountcenter.shop.model.Supply;
import com.jsmile.cloud.accountcenter.shop.repo.SupplyRepository;
import com.jsmile.cloud.accountcenter.shop.repo.SupplySearchRepository;
import com.jsmile.cloud.authc.api.feign.UserServiceApi;
import com.jsmile.cloud.authc.api.req.ReqSysUserRegister;
import com.jsmile.cloud.authc.api.res.ResSysUser;
import com.jsmile.mall.api.JSmileResult;
import com.jsmile.mall.api.enums.JSmileCacheEnum;
import com.jsmile.mall.api.exception.ExceptionKit;
import com.jsmile.mall.cache.kit.SpringKit;

import cn.hutool.core.bean.BeanUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
@AllArgsConstructor
public class SupplyApplicationService {

    private final SupplyRepository supplyRepository;
    private final SupplySearchRepository supplySearchRepository;

    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public ResSupply addSupply(ReqSupplyCreate reqSupplyCreate) {
        log.info("新增供应商:{]", reqSupplyCreate);
        Supply supply = Supply.create(reqSupplyCreate);
        ReqSysUserRegister sysUserRegisterTo = supply.registerUser();
        JSmileResult<ResSysUser> result = SpringKit.getBean(UserServiceApi.class).register(sysUserRegisterTo);// 注册供应商用户
        if (!result.getResSuccess()) {
            throw new AccountException(result.getResCode(), result.getResMsg(), null);
        }
        supplyRepository.save(supply);
        return BeanUtil.toBean(supply, ResSupply.class);
    }

    @CacheEvict(value = JSmileCacheEnum.JSmileCacheName.oneMonth, key = "'supply_'+#p0.supplyId")
    public ResSupply updateSupply(ReqSupplyUpdate reqSupplyUpdate) {
        log.info("更新供应商:{]", reqSupplyUpdate);
        Supply supply = supplySearchRepository.getById(reqSupplyUpdate.getSupplyId());
        if (null == supply) {
            throw new AccountException(AccountCodeEnum.SUPPLY_NOT_EXISTS);
        }
        supply.updateSupply(reqSupplyUpdate);
        supplyRepository.updateById(supply);
        return BeanUtil.toBean(supply, ResSupply.class);
    }

    @Cacheable(value = JSmileCacheEnum.JSmileCacheName.oneMonth, key = "'supply_'+#supplyId")
    public ResSupply getSupply(long supplyId) {
        log.info("获取供应商:{]", supplyId);
        Supply supply = supplyRepository.getById(supplyId);
        if (null == supply) {
            throw new AccountException(AccountCodeEnum.SUPPLY_NOT_EXISTS);
        }
        return BeanUtil.toBean(supply, ResSupply.class);
    }

    public IPage<ResSupply> getSupplyPageList(ReqSupplyQuery reqSupplyQuery) {
        ExceptionKit.checkNotNull(reqSupplyQuery.getCurTenantId(), "租户id不能为空!");
        log.info("供应商查询:{]", reqSupplyQuery);
        return supplySearchRepository.pageQuery(reqSupplyQuery).convert(x -> {
            return BeanUtil.toBean(x, ResSupply.class);
        });
    }

}
